//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "Period.h"
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::Times::CPeriod::CPeriod (QL::Times::FrequencyEnum f) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPeriod = NULL;
#endif
        QuantLib::Frequency _f = (QuantLib::Frequency)f ;
        _ppPeriod = new boost::shared_ptr<QuantLib::Period> (new QuantLib::Period ( _f ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CPeriod::CPeriod (Int32 n, QL::Times::TimeUnitEnum units) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPeriod = NULL;
#endif
        QuantLib::Integer _n = (QuantLib::Integer)ValueHelper::Convert (n); //d
        QuantLib::TimeUnit _units = (QuantLib::TimeUnit)units ;
        _ppPeriod = new boost::shared_ptr<QuantLib::Period> (new QuantLib::Period ( _n,  _units ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CPeriod::CPeriod () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPeriod = NULL;
#endif
        _ppPeriod = new boost::shared_ptr<QuantLib::Period> (new QuantLib::Period ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CPeriod::CPeriod (boost::shared_ptr<QuantLib::Period>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPeriod = NULL;
#endif
	_ppPeriod = &childNative;
    
}
Cephei::QL::Times::CPeriod::CPeriod (QuantLib::Period& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPeriod = NULL;
#endif
	_ppPeriod = new boost::shared_ptr<QuantLib::Period> (&childNative);
    
    _PeriodOwner = owner;
    
}

Cephei::QL::Times::CPeriod::CPeriod (CPeriod^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPeriod = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPeriod = new boost::shared_ptr<QuantLib::Period> (copy->GetShared());
        
    }
}
Cephei::QL::Times::CPeriod::CPeriod (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPeriod = NULL;
#endif
	if (!t->IsSubclassOf(CPeriod::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::CPeriod::CPeriod (QuantLib::Handle<QuantLib::Period>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPeriod = &childNative;
	_ppPeriod = &static_cast<boost::shared_ptr<QuantLib::Period>>(childNative.currentLink());
    
    _PeriodOwner = owner;
}
Cephei::QL::Times::CPeriod::CPeriod (QuantLib::Handle<QuantLib::Period> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPeriod = &childNative;
	_ppPeriod = &static_cast<boost::shared_ptr<QuantLib::Period>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Times::CPeriod::CPeriod (QuantLib::Period childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPeriod = NULL;
#endif
	_ppPeriod = new boost::shared_ptr<QuantLib::Period> (new QuantLib::Period (childNative));
    
}
#endif

Cephei::QL::Times::CPeriod::~CPeriod ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPeriod != NULL)
    {
	    delete _ppPeriod;
        _ppPeriod = NULL;
    }
}
Cephei::QL::Times::CPeriod::!CPeriod ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPeriod != NULL)
    {
	    delete _ppPeriod;
    }
}
QuantLib::Period& Cephei::QL::Times::CPeriod::GetReference ()
{
    if (_ppPeriod == NULL) throw REFNEW NativeNullException ();
	return **_ppPeriod;
}
boost::shared_ptr<QuantLib::Period>& Cephei::QL::Times::CPeriod::GetShared ()
{
    if (_ppPeriod == NULL) throw REFNEW NativeNullException ();
	return *_ppPeriod;
}
QuantLib::Period* Cephei::QL::Times::CPeriod::GetPointer ()
{
    if (_ppPeriod == NULL) throw REFNEW NativeNullException ();
	return &**_ppPeriod;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Period>& Cephei::QL::Times::CPeriod::GetHandle ()
{
	if (_phPeriod == NULL)
	{
		_phPeriod = new Handle<QuantLib::Period> (*_ppPeriod);
	}
	return *_phPeriod;
}
#endif
bool Cephei::QL::Times::CPeriod::HasNative () 
{
	return (_ppPeriod != NULL);
}

QL::Times::FrequencyEnum Cephei::QL::Times::CPeriod::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppPeriod)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Times::CPeriod::Length::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppPeriod)->length ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Times::CPeriod::Normalize::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppPeriod)->normalize ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::TimeUnitEnum Cephei::QL::Times::CPeriod::Units::get ()
{
    try
    {
    	QuantLib::TimeUnit _rv = (QuantLib::TimeUnit)(*_ppPeriod)->units ( );   
        QL::Times::TimeUnitEnum _nrv = (QL::Times::TimeUnitEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::CPeriodVector::CPeriodVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::Times::CPeriodVector::~CPeriodVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::Times::CPeriodVector::!CPeriodVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::Times::CPeriodVector::CPeriodVector (std::vector<QuantLib::Period>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::Times::CPeriodVector::CPeriodVector (std::vector<QuantLib::Period> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::Period> (vec);
}
#endif
Cephei::QL::Times::CPeriodVector::CPeriodVector (std::vector<boost::shared_ptr<QuantLib::Period>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::Times::CPeriodVector::CPeriodVector (std::vector<QuantLib::Handle<QuantLib::Period>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::Times::CPeriodVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Times::CPeriodVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw REFNEW RangeException ();
		QuantLib::Period& native = (*_pVValue)[index];
		return REFNEW CPeriod (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw REFNEW RangeException ();
		QuantLib::Period& native = *(*_pVBoost)[index];
		return REFNEW CPeriod (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw REFNEW RangeException ();
		QuantLib::Period& native = *((*_pVHand)[index].currentLink ());
		return REFNEW CPeriod (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::Times::CPeriodVector::default::set (int index, Cephei::QL::Times::IPeriod^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CPeriod^>(value)->GetReference ());
		}
        else
        {
            QuantLib::Period& c = (*_pVValue)[index];
            c = dynamic_cast<CPeriod^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CPeriod^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::Period>& c = (*_pVBoost)[index];
		c = dynamic_cast<CPeriod^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw REFNEW RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::Period> ());
		}
		QuantLib::Handle<QuantLib::Period>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::Period>& b = static_cast<boost::shared_ptr<QuantLib::Period>>(c.currentLink());
		b = dynamic_cast<CPeriod^>(value)->GetShared ();
	}
#endif
}
Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Times::CPeriodVector::CItem::get (int index) 
{
	return CellFactory::Instance->Create<IPeriod^> (dynamic_cast<IPeriod^>(default[index]));
}

void Cephei::QL::Times::CPeriodVector::CItem::set (int index, Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^ value) 
{
	default[index] = safe_cast<IPeriod^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::Period>& Cephei::QL::Times::CPeriodVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::Period>>& Cephei::QL::Times::CPeriodVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::Period>>& Cephei::QL::Times::CPeriodVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::Times::CPeriodVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::Period> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::Period>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::Period>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::IPeriod^ Cephei::QL::Times::CPeriod_Factory::Create (QL::Times::FrequencyEnum f)
{
    return REFNEW CPeriod ( f);
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Times::CPeriod_Factory::Create (Int32 n, QL::Times::TimeUnitEnum units)
{
    return REFNEW CPeriod ( n,  units);
}
Cephei::QL::Times::IPeriod^ Cephei::QL::Times::CPeriod_Factory::Create ()
{
    return REFNEW CPeriod ();
}
/* <summary>Create a single dimension array of Period that can be passed into other function </summary> */
IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Times::CPeriod_Factory::CreateVector ()
 {
	 return REFNEW CoVector<IPeriod^> (REFNEW CPeriodVector ());
 }
IVector<Cephei::QL::Times::IPeriod^>^ Cephei::QL::Times::CPeriod_Factory::CreateVector (PLATFORM::Collections::Generic::IEnumerable<IPeriod^>^ source)
 {
	 return REFNEW CoVector<IPeriod^> (source, REFNEW CPeriodVector ());
 }
/* <summary>Create a single dimension array of Period with change events</summary> */
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^>^>^ Cephei::QL::Times::CPeriod_Factory::CreateCellVector ()
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^>^> (REFNEW CoCellVector<Cephei::QL::Times::IPeriod^> (REFNEW CPeriodVector ()));
 }
Cephei::Core::Generic::ICoCell<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^>^>^ Cephei::QL::Times::CPeriod_Factory::CreateCellVector (PLATFORM::Collections::Generic::IEnumerable<Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^>^ source)
 {
	 return CellFactory::Instance->Create<IVector<Cephei::Core::Generic::ICoCell<Cephei::QL::Times::IPeriod^>^>^> (REFNEW CoCellVector<Cephei::QL::Times::IPeriod^> (source, REFNEW CPeriodVector ()));
 }

